package com.dianping.cat.consumer.event.model.transform;

import com.dianping.cat.consumer.event.model.entity.EventName;
import com.dianping.cat.consumer.event.model.entity.EventReport;
import com.dianping.cat.consumer.event.model.entity.EventType;
import com.dianping.cat.consumer.event.model.entity.GraphTrend;
import com.dianping.cat.consumer.event.model.entity.Machine;
import com.dianping.cat.consumer.event.model.entity.Range;
import com.dianping.cat.consumer.event.model.entity.StatusCode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.xml.sax.Attributes;












public class DefaultSaxMaker
  extends Object
  implements IMaker<Attributes>
{
  public String buildDomain(Attributes attributes) { throw new UnsupportedOperationException(); }



  public EventReport buildEventReport(Attributes attributes) {
    String domain = attributes.getValue("domain");
/*  37 */     String startTime = attributes.getValue("startTime");
/*  38 */     String endTime = attributes.getValue("endTime");
/*  39 */     EventReport eventReport = new EventReport(domain);

/*  41 */     if (startTime != null) {
/*  42 */       eventReport.setStartTime(toDate(startTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  45 */     if (endTime != null) {
/*  46 */       eventReport.setEndTime(toDate(endTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  49 */     return eventReport;
  }


  public GraphTrend buildGraphTrend(Attributes attributes) {
/*  54 */     String duration = attributes.getValue("duration");
/*  55 */     String count = attributes.getValue("count");
/*  56 */     String fails = attributes.getValue("fails");
/*  57 */     GraphTrend graphTrend = new GraphTrend((duration == null) ? 0 : ((Integer)convert(Integer.class, duration, Integer.valueOf(0))).intValue());

/*  59 */     if (count != null) {
/*  60 */       graphTrend.setCount(count);
    }

/*  63 */     if (fails != null) {
/*  64 */       graphTrend.setFails(fails);
    }

/*  67 */     return graphTrend;
  }



/*  72 */   public String buildIp(Attributes attributes) { throw new UnsupportedOperationException(); }



  public Machine buildMachine(Attributes attributes) {
/*  77 */     String ip = attributes.getValue("ip");
    return new Machine(ip);
  }




  public EventName buildName(Attributes attributes) {
/*  85 */     String id = attributes.getValue("id");
/*  86 */     String totalCount = attributes.getValue("totalCount");
/*  87 */     String failCount = attributes.getValue("failCount");
/*  88 */     String failPercent = attributes.getValue("failPercent");
/*  89 */     String tps = attributes.getValue("tps");
/*  90 */     EventName name = new EventName(id);

/*  92 */     if (totalCount != null) {
/*  93 */       name.setTotalCount(((Long)convert(Long.class, totalCount, Long.valueOf(0L))).longValue());
    }

/*  96 */     if (failCount != null) {
/*  97 */       name.setFailCount(((Long)convert(Long.class, failCount, Long.valueOf(0L))).longValue());
    }

/* 100 */     if (failPercent != null) {
/* 101 */       name.setFailPercent(toNumber(failPercent, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 104 */     if (tps != null) {
/* 105 */       name.setTps(toNumber(tps, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 108 */     return name;
  }


  public Range buildRange(Attributes attributes) {
/* 113 */     String value = attributes.getValue("value");
/* 114 */     String count = attributes.getValue("count");
/* 115 */     String fails = attributes.getValue("fails");
/* 116 */     Range range = new Range((value == null) ? null : (Integer)convert(Integer.class, value, null));

/* 118 */     if (count != null) {
/* 119 */       range.setCount(((Integer)convert(Integer.class, count, Integer.valueOf(0))).intValue());
    }

/* 122 */     if (fails != null) {
/* 123 */       range.setFails(((Integer)convert(Integer.class, fails, Integer.valueOf(0))).intValue());
    }

/* 126 */     return range;
  }


  public StatusCode buildStatusCode(Attributes attributes) {
/* 131 */     String id = attributes.getValue("id");
/* 132 */     String count = attributes.getValue("count");
/* 133 */     StatusCode statusCode = new StatusCode(id);

/* 135 */     if (count != null) {
/* 136 */       statusCode.setCount(((Long)convert(Long.class, count, Long.valueOf(0L))).longValue());
    }

/* 139 */     return statusCode;
  }


  public EventType buildType(Attributes attributes) {
/* 144 */     String id = attributes.getValue("id");
/* 145 */     String totalCount = attributes.getValue("totalCount");
/* 146 */     String failCount = attributes.getValue("failCount");
/* 147 */     String failPercent = attributes.getValue("failPercent");
/* 148 */     String tps = attributes.getValue("tps");
/* 149 */     EventType type = new EventType(id);

/* 151 */     if (totalCount != null) {
/* 152 */       type.setTotalCount(((Long)convert(Long.class, totalCount, Long.valueOf(0L))).longValue());
    }

/* 155 */     if (failCount != null) {
/* 156 */       type.setFailCount(((Long)convert(Long.class, failCount, Long.valueOf(0L))).longValue());
    }

/* 159 */     if (failPercent != null) {
/* 160 */       type.setFailPercent(toNumber(failPercent, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 163 */     if (tps != null) {
/* 164 */       type.setTps(toNumber(tps, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 167 */     return type;
  }


  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 172 */     if (value == null || value.length() == 0) {
/* 173 */       return defaultValue;
    }

/* 176 */     if (type == Boolean.class || type == boolean.class)
/* 177 */       return (T)Boolean.valueOf(value); 
/* 178 */     if (type == Integer.class || type == int.class)
/* 179 */       return (T)Integer.valueOf(value); 
/* 180 */     if (type == Long.class || type == long.class)
/* 181 */       return (T)Long.valueOf(value); 
/* 182 */     if (type == Short.class || type == short.class)
/* 183 */       return (T)Short.valueOf(value); 
/* 184 */     if (type == Float.class || type == float.class)
/* 185 */       return (T)Float.valueOf(value); 
/* 186 */     if (type == Double.class || type == double.class)
/* 187 */       return (T)Double.valueOf(value); 
/* 188 */     if (type == Byte.class || type == byte.class)
/* 189 */       return (T)Byte.valueOf(value); 
/* 190 */     if (type == Character.class || type == char.class) {
/* 191 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 193 */     return (T)value;
  }


  protected Date toDate(String str, String format, Date defaultValue) {
/* 198 */     if (str == null || str.length() == 0) {
/* 199 */       return defaultValue;
    }

    try {
/* 203 */       return (new SimpleDateFormat(format)).parse(str);
/* 204 */     } catch (ParseException e) {
/* 205 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format, Number defaultValue) {
/* 210 */     if (str == null || str.length() == 0) {
/* 211 */       return defaultValue;
    }

    try {
/* 215 */       return (new DecimalFormat(format)).parse(str);
/* 216 */     } catch (ParseException e) {
/* 217 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/event/model/transform/DefaultSaxMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */